//
//  ListViewController.m
//  喜马拉雅改良版框架
//
//  Created by liuxin on 15/12/22.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ListViewController.h"

@interface ListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)UIImageView *headImageView;
@property(nonatomic,strong)NSMutableArray *allArray;
@end

@implementation ListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.allArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor orangeColor];
    [self getData];
    [self createHeadImage];
    [self createTableView];
}
-(void)createTableView{
    self.myTableView = [[UITableView alloc]init];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    _myTableView.tableHeaderView = _headImageView;
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    [_myTableView registerClass:[LXListTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_myTableView];
    [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(@0);
        make.bottom.mas_equalTo(-50);
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[_allArray[section] list] count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LXListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.cellModel = [[_allArray objectAtIndex:indexPath.section] list][indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat sectionHeaderHeight = 30;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    }
    else if (scrollView.contentOffset.y>=sectionHeaderHeight)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _allArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    UIImageView *imageV = [[UIImageView alloc]init];
    imageV.image = [UIImage imageNamed:@"findsection_logo@2x.png"];
    [header addSubview:imageV];
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(5);
        make.bottom.mas_equalTo(-5);
        make.width.mas_equalTo(10);
    }];
  
    UILabel *label = [[UILabel alloc]init];
    [header addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(imageV.mas_right).offset(5);
        make.width.mas_equalTo(80);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    label.text = [_allArray[section] title];
    return header;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 85;
}
-(void)getData{
    NSString *url = @"http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/group?channel=and-d10&device=iphone&includeActivity=true&includeSpecial=true&scale=2&version=4.3.2";
    [AFNetworkTool getUrl:url body:nil result:HXJSON headerFile:nil success:^(id result) {
        NSDictionary *headImageDic = result[@"focusImages"];
        NSArray *listArr = [NSArray arrayWithArray:headImageDic[@"list"]];
        NSDictionary *imageInfo = listArr[0];
        [_headImageView sd_setImageWithURL:[NSURL URLWithString:imageInfo[@"pic"]] placeholderImage:[UIImage imageNamed:ImagePlaceHolder]];
        
        NSArray *array = [NSArray arrayWithArray:result[@"datas"]];
        for (NSDictionary *dic in array) {
            LXListModel *temp = [LXListModel new];
            [temp setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:temp];
        }
        [self.myTableView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"数据请求失败-->%@",error);
    }];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0 && indexPath.row == 0) {
        LXHotMusicTableVC *hot = [[LXHotMusicTableVC alloc]init];
        hot.NavigationTitle = [[[[_allArray objectAtIndex:indexPath.section] list] objectAtIndex:indexPath.row] title];
        hot.key = [[[[_allArray objectAtIndex:indexPath.section] list] objectAtIndex:indexPath.row] key];
        hot.contentType = [[[[_allArray objectAtIndex:indexPath.section] list] objectAtIndex:indexPath.row] contentType];
        [self.navigationController pushViewController:hot animated:YES];
    }else if ((indexPath.section == 0&&indexPath.row==1)||(indexPath.section == 0 &&indexPath.row==2)||(indexPath.section == 0 &&indexPath.row==3)) {
        LXHotTableViewController *hot = [LXHotTableViewController new];
        hot.NavigationTitle = [[[[_allArray objectAtIndex:indexPath.section] list] objectAtIndex:indexPath.row] title];
        hot.key = [[[[_allArray objectAtIndex:indexPath.section] list] objectAtIndex:indexPath.row] key];
        hot.contentType = [[[[_allArray objectAtIndex:indexPath.section] list] objectAtIndex:indexPath.row] contentType];
        [self.navigationController pushViewController:hot animated:YES];
    }else{
        LXHotAuthorTableVC *hot = [LXHotAuthorTableVC new];
        hot.NavigationTitle = [[[[_allArray objectAtIndex:indexPath.section] list] objectAtIndex:indexPath.row] title];
        hot.key = [[[[_allArray objectAtIndex:indexPath.section] list] objectAtIndex:indexPath.row] key];
        hot.contentType = [[[[_allArray objectAtIndex:indexPath.section] list] objectAtIndex:indexPath.row] contentType];
        [self.navigationController pushViewController:hot animated:YES];
    }
}
-(void)createHeadImage{
    self.headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 170)];
    _headImageView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_headImageView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
